function addTicket(anchor) {
	var e1 = $('<div></div>');
	var e2 = $('<span>Min</span>');
	var e3 = $('<input type="text" />');
	var e4 = $('<span class="max">Max</span>');
	var e5 = $('<input class="maxInput" type="text" />');
	var e6 = $('<a href="#" class="ticketItemDelete">x</a>')
	e3.type='text';
	e1.addClass('ticketItem');
	e1.append(e2).append(e3).append(e4).append(e5).append(e6);
	
	e1.appendTo(anchor);
	e5.bind('change', function() {
		
			addGames(e1, e5.val());
		
	});
	e6.bind('click', function() {
		removeTicketItem(e6);
	});
}

function removeTicketItem(element) {
	element.parent().remove();
}

function addGames(anchor, count) {
	var currentItems = anchor.has('div.ticketItemGame').children('div.ticketItemGame').length;
	
	if (currentItems < count) {
		for (i = currentItems; i < count; i++)
		{
			var a1 = $('<div class="ticketItemGame"></div>');
			var a2 = $('<span> Natprevar x</span>');
			var a3 = $('<input type="text" />');
			a1.append(a2).append(a3);	
			a1.appendTo(anchor);
		}
	}
	else {
		
			console.log(anchor.children('div.ticketItemGame'));
			console.log(currentItems);
			console.log(count);
			for (i = 0; i < count; i++)
			{
				console.log(anchor.children('div.ticketItemGame')[i]);
				anchor.children('div.ticketItemGame').slice(0,1);
			}
		
			
	}
	
}

$(document).ready(function () {
	
	var anchor = $(".wrapper");
	var addItem = $(".addItem");
	var deleteTicketItem = $(".ticketItemDelete");
	
	
	
	
	addItem.bind('click', function() {
		
		addTicket(anchor);
	});
	
	

    $.ajax({
        type: "POST",
        url: "http://gjavolce.ath.cx/Services/TicketCalculator.svc/TestJSON",
        data: "{2.0}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            alert(data.d);
        }
    });

	
	
	
});